
/**
 *
 */
package com.serviceprovider.business.module.admin.transaction.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.model.NewCsvReplica;
import com.serviceprovider.business.module.admin.transaction.services.dao.interfaces.IFlatTableService;
import com.serviceprovider.business.module.admin.transaction.services.dao.interfaces.ITransactionImportDataService;
import com.serviceprovider.business.module.admin.transaction.utility.StringUtils;
import com.serviceprovider.business.utility.RequestConstants;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * The Class TransactionCSVDataViewController.
 *
 * @author mrduongnv@gmail.com
 */
@Controller
public class TransactionCSVDataViewController {

    /** The Constant MIX_LIST. */
    private static final String MIX_LIST = "mixList";

    /** The Constant SUCCESS_LIST. */
    private static final String SUCCESS_LIST = "successList";

    /** The logger. */
    private final Logger      logger = Logger.getLogger(getClass());
    @Autowired
    private IFlatTableService flatTableService;

    /** The transaction import data service. */
    @Autowired
    private ITransactionImportDataService transactionImportDataService;

    /**
     * Show list.
     *
     * @param request
     *            the request
     * @return the model and view
     */
    @ModelAttribute
    @RequestMapping(
        value  = RequestConstants.TransactionRequests.VIEW_TRANSACTIONCSV_RECORD,
        method = RequestMethod.GET
    )
    public ModelAndView showList(HttpServletRequest request) {
        final String fileId = request.getParameter("file_id");

        if (StringUtils.isEmpty(fileId) ||!StringUtils.isAllDigits(fileId)) {
            return new ModelAndView(RequestConstants.TransactionRequests.TRANSACTION_SUMMARY_VIEW);
        }

        ModelAndView mov = new ModelAndView(RequestConstants.TransactionRequests.V_VIEW_TRANSACTIONCSV_RECORD);

        try {
            boolean                   processed = true;
            final List<NewCsvReplica> results   =
                this.transactionImportDataService.retrieveImportedTransactionDataListByHistoryId(
                    Integer.valueOf(fileId));

            for (NewCsvReplica newCsvReplica : results) {
                if ((newCsvReplica == null) ||!newCsvReplica.isProcessed()) {
                    processed = false;

                    break;
                }
            }

            mov.addObject(SUCCESS_LIST, results);

            if (!processed) {
                String url = "transform_transaction_data.htm?historyId=" + fileId;

                mov.addObject("url", url);
            } else {
                mov.addObject("info", "All the records are processed.");
            }
        } catch (Exception e) {
            this.logger.error("Error occured when records are displayed.", e);
            mov.addObject("message", "Error occured when records are displayed.");
        }

        return mov;
    }
}



